package com.ds.robot.assistant.config;

import com.ds.robot.assistant.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 作用：
 *
 * @author WeiShaoying
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    // @Autowired
    // private TokenOncePerRequestFilter tokenOncePerRequestFilter;
    //
    // @Autowired
    // private TokenFilter tokenFilter;

    @Override
    public void addCorsMappings(org.springframework.web.servlet.config.annotation.CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true);
    }

    @Override
    public void addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    //注册拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor).excludePathPatterns("/api/**", "/robot/**", "/test/**");
    }

    // @Bean
    // public FilterRegistrationBean<TokenOncePerRequestFilter> filterRegistrationBean() {
    //     FilterRegistrationBean<TokenOncePerRequestFilter> registrationBean = new FilterRegistrationBean<>();
    //     registrationBean.setFilter(tokenOncePerRequestFilter);
    //     registrationBean.addUrlPatterns("/req/milestone/*");
    //     registrationBean.setOrder(1);
    //     return registrationBean;
    // }

    // @Bean
    // public FilterRegistrationBean<TokenFilter> filterRegistrationBean2() {
    //     FilterRegistrationBean<TokenFilter> registrationBean = new FilterRegistrationBean<>();
    //     registrationBean.setFilter(tokenFilter);
    //     registrationBean.addUrlPatterns("/req/milestone/*");
    //     return registrationBean;
    // }
}
